<template>
	<view class="publish_wrap">
		<!-- <view class="flex justify-between items-center section_5">
			<view class="flex">
				<text class="font">选择分类</text>
				<picker mode="selector" :value="type_index" :range="type_list" range-key="title" @change="bindType">
					<view class="text_3">{{ type_index === '' ? '请选择需求或服务' : type_list[type_index].title }}</view>
				</picker>
			</view>
			<image class="image_8" :src="remoteUrl + 'right_icon.png'" />
		</view> -->
		<view class="flex justify-between items-center section_5">
			<view class="flex">
				<text class="font">需求标题</text>
				<input class="text_3" type="text" placeholder="请输入" :value="title" @input="bindTitle" />
			</view>
		</view>
		<view class="flex justify-between items-center section_5">
			<view>
				<text class="font">需求内容</text>
				<textarea class="text_area" :value="content" maxlength="2000" placeholder="请输入" @input="bindcontent"></textarea>
			</view>
		</view>
		<view class="bot_btn" @click="upTask()" v-if="edit_id == ''">
			<view class="btn">发布</view>
		</view>
		<view class="bot_btn" @click="editTask()" v-else>
			<view class="btn">修改</view>
		</view>
	</view>
</template>

<script>
	import {
		remoteUrl
	} from '@/utils/config.js'
	import {
		saveTask,
		updateTask
	} from '@/api/home.js'
	export default {
		data() {
			return {
				remoteUrl: remoteUrl,
				type_list: [
					{
						title: '供求'
					},
					{
						title: '需求'
					}
				],
				type_index: '',
				title: '',
				content: '',
				edit_id: '',
				status: ''
			};
		},
		onLoad(options) {
			if (options.detail) {
				let obj = JSON.parse(options.detail)
				this.title = obj.title
				this.type_index = parseInt(obj.type_id) - 1
				this.content = obj.content
				this.edit_id = obj.id
				this.status = obj.status
			}
		},
		onShareAppMessage(res) { //发送给朋友
			return {
				title: '小程序分享',
				imageUrl: '',
			}
		},
		methods: {
			bindType(e) {
				this.type_index = e.detail.value
			},
			bindTitle(e) {
				this.title = e.detail.value
			},
			bindcontent(e) {
				this.content = e.detail.value
			},
			upTask() {
				// if (this.type_index == '') {
				// 	uni.showToast({
				// 		title: '请选择供需分类！',
				// 		icon: 'none'
				// 	})
				// 	return
				// }
				if (this.title == '') {
					uni.showToast({
						title: '请输入供需标题！',
						icon: 'none'
					})
					return
				}
				if (this.content == '') {
					uni.showToast({
						title: '请输入内容！',
						icon: 'none'
					})
					return
				}
				if (!uni.getStorageSync('userInfo')) {
					uni.showToast({
						title: '请先进行登录！',
						icon: 'none'
					})
					return
				}
				saveTask({
					title: this.title,
					content: this.content,
					type_id: 2
				}).then(res => {
					if (res.code == 1) {
						uni.showToast({
							title: '提交成功！',
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateBack()
						},600)
					}else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			},
			editTask() {
				updateTask({
					title: this.title,
					content: this.content,
					type_id: 2,
					id: this.edit_id
				}).then(res => {
					if (res.code == 1) {
						uni.showToast({
							title: '提交成功！',
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateBack()
						},600)
					}else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.publish_wrap {
		height: 100vh;
		background: #F5F5F5;
		padding-top: 30rpx;
		.section_5 {
			width: 690rpx;
			margin: 0 auto 20rpx;
			padding: 40rpx 28rpx 42rpx;
			background-color: #ffffff;
			border-radius: 8rpx;

			.font {
				font-size: 28rpx;
				font-weight: bold;
			}

			.text_3 {
				opacity: 0.5;
				font-size: 28rpx;
				margin-left: 50rpx;
			}

			.image_8 {
				opacity: 0.5;
				width: 12rpx;
				height: 24rpx;
			}
			.text_area {
				margin-top: 20rpx;
			}
		}
	
		.bot_btn {
			width: 100vw;
			height: 150rpx;
			background: #FFFFFF;
			position: fixed;
			bottom: 0;
			left: 0;
			z-index: 2;
			display: flex;
			align-items: center;
		
			.btn {
				width: 650rpx;
				height: 80rpx;
				line-height: 80rpx;
				margin: 0 auto;
				text-align: center;
				color: #fff;
				font-size: 28rpx;
				background: #1C41F1;
				border-radius: 54rpx;
			}
		}
	}
</style>